package GUI; //done

import java.awt.Rectangle;
import java.awt.SystemColor;
import java.awt.TextArea;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client.ConnectionToServer;
import Entities.DealSale;

/**
 * This Class controls the Schedule Window.
 * @author ameer
 * @author waleed
 * @author bshara
 * @author kamil
 */
public class ScheduleWindow extends JPanel{
	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;

	/** The customers jLabel. */
	private JLabel clientsjLabel = null;
	
	/** The Customers JComboBox. */
	private JComboBox CustomersJComboBox = null;
	
	/** The Sales Men jLabel. */
	private JLabel SalesMenjLabel = null;
	
	/** The Sales Men JComboBox. */
	private JComboBox SalesMenjComboBox = null;
	
	/** The Auto CheckBox. */
	private JCheckBox autojCheckBox = null;
	
	/** The Number of Customres Label. */
	private JLabel numOfCustomersjLabel = null;
	
	/** The Number of Customers Text Field. */
	private JTextField numOfCustomersjTextField = null;
	
	/** The Total Customers Label. */
	private JLabel totalCustomersjLabel = null;
	
	/** The Total Customers Text Field. */
	private JTextField totalCustomersjTextField = null;
	
	/** The Total Sales Men jLabel. */
	private JLabel totalSalesMenjLabel = null;
	
	/** The Total Sales Men Text Field. */
	private JTextField totalSalesMenjTextField = null;
	
	/** The Submited Text Area. */
	private TextArea submitedTextArea = null;
	
	/** The Submited jLabel. */
	private JLabel submitedjLabel = null;
	
	/** The Submited Text Field. */
	private JTextField submitedjTextField = null;
	
	/** The left jLabel. */
	private JLabel leftjLabel = null;
	
	/** The left Text Field. */
	private JTextField leftjTextField = null;
	
	/** The Submit Button. */
	private JButton submitjButton = null;
	
	/** The Finish Button. */
	private JButton finishjButton = null;
	
	/** The DealSale. */
	private DealSale dealsale = null;
	
	/** The Product ID. */
	private long prodID = 0;
	
	/**
	 * The default constructor 
	 */
	public ScheduleWindow(DealSale dealsale ) {
		super();
		setBackground(SystemColor.activeCaption);
		this.dealsale = dealsale;	
		initialize();
	}

	/**
	 * This function initializes ScheduleWindow
	 */
	private void initialize() {
		leftjLabel = new JLabel();
		leftjLabel.setBounds(new Rectangle(267, 222, 48, 22));
		leftjLabel.setText("Left:");
		
		submitedjLabel = new JLabel();
		submitedjLabel.setBounds(new Rectangle(267, 189, 65, 22));
		submitedjLabel.setText("Submited:");
		
		totalSalesMenjLabel = new JLabel();
		totalSalesMenjLabel.setBounds(new Rectangle(267, 82, 125, 22));
		totalSalesMenjLabel.setText("Number of Sales men");
		
		totalCustomersjLabel = new JLabel();
		totalCustomersjLabel.setBounds(new Rectangle(267, 146, 107, 22));
		totalCustomersjLabel.setText("Number of client:");
		
		numOfCustomersjLabel = new JLabel();
		numOfCustomersjLabel.setBounds(new Rectangle(76, 7, 137, 22));
		numOfCustomersjLabel.setText("Customers per Sales man:");
		
		SalesMenjLabel = new JLabel();
		SalesMenjLabel.setBounds(new Rectangle(267, 49, 78, 22));
		SalesMenjLabel.setText("Sales men");
		
		clientsjLabel = new JLabel();
		clientsjLabel.setBounds(new Rectangle(267, 113, 96, 22));
		clientsjLabel.setText("Clients:");
		
		this.setLayout(null);
		this.setSize(536, 318);
		this.add(clientsjLabel, null);
		this.add(getCustomersjComboBox(), null);
		this.add(SalesMenjLabel, null);
		this.add(getSalesMenjComboBox(), null);
		this.add(getAutojCheckBox(), null);
		this.add(numOfCustomersjLabel, null);
		this.add(getNumofclientjTextField(), null);
		this.add(totalCustomersjLabel, null);
		this.add(getTotalclientsjTextField(), null);
		this.add(totalSalesMenjLabel, null);
		this.add(getTotalselmensjTextField(), null);
		this.add(getSubmitetextArea(), null);
		this.add(submitedjLabel, null);
		this.add(getSubmitedjTextField(), null);
		this.add(leftjLabel, null);
		this.add(getLeftjTextField(), null);
		this.add(getSubmitjButton(), null);
		this.add(getFinishjButton(), null);		
		getCustomersjComboBox();
		getSalesMenjComboBox();
	}
	
	/**
	 * @return the dealsale
	 */
	public DealSale getDealSale(){
		return dealsale;
	}

	/**
	 * @param dealsale the dealsale to set
	 */
	public void setDealSale(DealSale dealsale){
		this.dealsale = dealsale;
	}
	
	/**
	 * @param prodID The Product ID
	 */
	public void setProdID(long prodID){
		this.prodID = prodID;
	}

	/**
	 * This function initializes CustomersJComboBox	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getCustomersjComboBox(){
		if (CustomersJComboBox == null) {
			CustomersJComboBox = new JComboBox();
			CustomersJComboBox.setBounds(new Rectangle(384, 113, 107, 22));			
		}
		return CustomersJComboBox;
	}
	
	/**
	 * This function loads all customers CustomersJComboBox
	 */
	public void updateCustomersjComboBox(){
		long[] clientlist = null;
		try{
			int i=0,itemcount = CustomersJComboBox.getItemCount();
			if (itemcount > 0) {
				CustomersJComboBox.addItem(0);
				CustomersJComboBox.setSelectedItem(0);				
			}
			for (i=0; i < itemcount ; i++ ){				
				CustomersJComboBox.removeItemAt(0);						
			}			
			ConnectionToServer connectionToserver = new ConnectionToServer();
			clientlist = (long[])connectionToserver.loadAllInterestedCustomers(prodID);			
			if (clientlist != null){
				int len = clientlist.length;
				for(i=0; i < len; i++){
					CustomersJComboBox.addItem(clientlist[i]);							
				}
				totalCustomersjTextField.setText(String.valueOf(len));
			}	
		}
		catch(Exception except){
			JOptionPane.showMessageDialog(null, "Error!","",JOptionPane.ERROR_MESSAGE);
		}				
	}

	/**
	 * This function loads all sales men to SalesMenjComboBox
	 */
	public void updateSalesMenjComboBox(){
		long[] SalesMenList = null;
		try{
			int i=0,itemcount = SalesMenjComboBox.getItemCount();
			if (itemcount > 0){
				SalesMenjComboBox.addItem(0);
				SalesMenjComboBox.setSelectedItem(0);				
			}
			for (i=0; i < itemcount ; i++ ){				
				SalesMenjComboBox.removeItemAt(0);						
			}			
			ConnectionToServer connectionToserver = new ConnectionToServer();
			SalesMenList = (long[])connectionToserver.loadAllSalesMen();			
			if (SalesMenList != null){
				int len = SalesMenList.length;
				for(i=0; i < len; i++){
					SalesMenjComboBox.addItem(SalesMenList[i]);							
				}
				totalSalesMenjTextField.setText(String.valueOf(len));
			}	
		}
		catch(Exception except){
			JOptionPane.showMessageDialog(null, "SYSTEM ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}		
	}
	
	/**
	 * This function initializes SalesMenjComboBox		
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSalesMenjComboBox(){
		if (SalesMenjComboBox == null){
			SalesMenjComboBox = new JComboBox();
			SalesMenjComboBox.setBounds(new Rectangle(384, 49, 107, 22));			
		}
		return SalesMenjComboBox;
	}

	/**
	 * This function initializes autojCheckBox	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getAutojCheckBox(){
		if (autojCheckBox == null) {
			autojCheckBox = new JCheckBox();
			autojCheckBox.setBackground(SystemColor.activeCaption);
			autojCheckBox.setBounds(new Rectangle(16, 7, 54, 22));
			autojCheckBox.setText("Auto");
			autojCheckBox.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (autojCheckBox.isSelected())
						numOfCustomersjTextField.setEditable(true);
					else
						numOfCustomersjTextField.setEditable(false);
				}
			});	
		}
		return autojCheckBox;
	}

	/**
	 * This function initializes numOfCustomersjTextField		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNumofclientjTextField() {
		if (numOfCustomersjTextField == null) {
			numOfCustomersjTextField = new JTextField();
			numOfCustomersjTextField.setBounds(new Rectangle(223, 7, 39, 22));
			numOfCustomersjTextField.setEditable(false);
		}
		return numOfCustomersjTextField;
	}

	/**
	 * This function initializes totalCustomersjTextField		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTotalclientsjTextField() {
		if (totalCustomersjTextField == null) {
			totalCustomersjTextField = new JTextField();
			totalCustomersjTextField.setBounds(new Rectangle(384, 146, 107, 22));
			totalCustomersjTextField.setEditable(false);
		}
		return totalCustomersjTextField;
	}

	/**
	 * This function initializes totalSalesMenjTextField	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTotalselmensjTextField() {
		if (totalSalesMenjTextField == null){
			totalSalesMenjTextField = new JTextField();
			totalSalesMenjTextField.setBounds(new Rectangle(384, 82, 107, 22));
			totalSalesMenjTextField.setEditable(false);
		}
		return totalSalesMenjTextField;
	}

	/**
	 * This function initializes submitedTextArea		
	 * @return java.awt.TextArea	
	 */
	private TextArea getSubmitetextArea(){
		if (submitedTextArea == null) {
			submitedTextArea = new TextArea();
			submitedTextArea.setBounds(new Rectangle(21, 49, 180, 181));
			submitedTextArea.setEditable(false);
		}
		return submitedTextArea;
	}

	/**
	 * This function initializes submitedjTextField		
	 * @return javax.swing.JTextField	
	 */
	private JTextField getSubmitedjTextField(){
		if (submitedjTextField == null) {
			submitedjTextField = new JTextField();
			submitedjTextField.setBounds(new Rectangle(352, 189, 65, 22));
			submitedjTextField.setEditable(false);
		}
		return submitedjTextField;
	}

	/**
	 * This function initializes leftjTextField	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getLeftjTextField(){
		if (leftjTextField == null){
			leftjTextField = new JTextField();
			leftjTextField.setBounds(new Rectangle(352, 222, 65, 22));
			leftjTextField.setEditable(false);
		}
		return leftjTextField;
	}

	/**
	 * This function initializes submitjButton	
	 * @return javax.swing.JButton	
	 */
	private JButton getSubmitjButton() {
		if (submitjButton == null) {
			submitjButton = new JButton();
			submitjButton.setBackground(SystemColor.scrollbar);
			submitjButton.setBounds(new Rectangle(138, 266, 96, 22));
			submitjButton.setText("submit");
			submitjButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					long cliet=0, sellsmen=0;
					String str = "";
					int i, j, numofsellsmen, numpersellsmen;
					if (autojCheckBox.isSelected()){
						try{
							numpersellsmen = Integer.parseInt(numOfCustomersjTextField.getText());
						}
						catch(NumberFormatException numexc){
							JOptionPane.showMessageDialog(null, "ILEGAL INPUT","",JOptionPane.WARNING_MESSAGE);
							return;
						}						
						numofsellsmen = SalesMenjComboBox.getItemCount();
						for(i=0; i < numofsellsmen; i++ ){
							sellsmen = (Long)SalesMenjComboBox.getItemAt(i);
							for (j=0; j < numpersellsmen; j++){
								if (CustomersJComboBox.getItemCount()==0)
									return;
								str = submitedTextArea.getText();
								cliet = (Long)CustomersJComboBox.getItemAt(0);
								if (sellsmen == 0 || cliet == 0)
									return;
								str = str+sellsmen+":"+cliet+".\n";
								submitedTextArea.setText(str);
								CustomersJComboBox.removeItem(cliet);
								leftjTextField.setText(String.valueOf(CustomersJComboBox.getItemCount()));
							}
						}						
					}
					else{
						if (CustomersJComboBox.getItemCount()==0)
							return;
						sellsmen = (Long)SalesMenjComboBox.getSelectedItem();
						cliet = (Long)CustomersJComboBox.getSelectedItem();
						if (sellsmen == 0 || cliet == 0)
							return;
						str = submitedTextArea.getText();
						str = str+sellsmen+":"+cliet+".\n";
						submitedTextArea.setText(str);
						CustomersJComboBox.removeItem(cliet);
						leftjTextField.setText(String.valueOf(CustomersJComboBox.getItemCount()));
						totalCustomersjTextField.setText(String.valueOf(CustomersJComboBox.getItemCount()));
						leftjTextField.setText(String.valueOf(CustomersJComboBox.getItemCount()));
					}
				}
			});	
		}
		return submitjButton;
	}

	/**
	 * This function initializes finishjButton		
	 * @return javax.swing.JButton	
	 */
	private JButton getFinishjButton(){
		if (finishjButton == null){
			finishjButton = new JButton();
			finishjButton.setBackground(SystemColor.scrollbar);
			finishjButton.setBounds(new Rectangle(302, 266, 90, 22));
			finishjButton.setText("finish");
			finishjButton.addActionListener(new java.awt.event.ActionListener() {
				String msg;
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (CustomersJComboBox.getItemCount() == 0 || 
							((CustomersJComboBox.getItemCount() == 1) && 
									(Long)CustomersJComboBox.getItemAt(0) == null)){
						try {					
							ConnectionToServer connectiontoserver = new ConnectionToServer();
							msg = (String)connectiontoserver.updateSalesManSchedule(submitedTextArea.getText(), dealsale.getDealSaleID());
							if (msg != null){
								if (msg.equals("OK"))
									JOptionPane.showMessageDialog(null, "Schedule succcessfuly saved");
								else
									JOptionPane.showMessageDialog(null, "Schedule not saved");							
							}
							else
								JOptionPane.showMessageDialog(null, "ERROR!!","",JOptionPane.ERROR_MESSAGE);
						} 
						catch (Exception e1){
							JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);
						}
					}
					else{
						JOptionPane.showMessageDialog(null, "Not all customers submited","",JOptionPane.WARNING_MESSAGE);
					}
				}
			});		
		}
		return finishjButton;
	}
}
